﻿using System;
using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Core.Security;
using Web.Core.Components;
using Ayatta.Contract.Domain;

namespace Web.Pay.Controllers
{
    public class PayController : BaseController
    {
        public PayController(Lazy<IInfrastructureService> infrastructure, Lazy<IPassportService> passport, Lazy<IAccountService> account, Lazy<IStoreService> store, Lazy<ITradeService> trade, Lazy<IPlayService> play, Lazy<IMiscService> misc, Lazy<IExtraService> extra, IMemcachedClient memcachedClient)
            : base(infrastructure, passport, account, store, trade, play, misc, extra, memcachedClient)
        {
        }

        /// <summary>
        /// 使用网银支付
        /// </summary>
        /// <param name="param">订单号</param>
        /// <param name="eBankId">电子银行Id</param>
        /// <param name="useBalance">是否使用帐户余额</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EBank(string param, int eBankId, bool useBalance = false)
        {
            var time = DateTime.Now;
            var url = WebSite.My + "/order";
            var identity = User.Identity.Identity();
            var order = Trade.Value.GetOrder(param, identity.Id);
            if (order != null)
            {
                if (order.Status == OrderStatus.Pending)
                {
                    return Redirect(WebSite.My + "/order/pay/" + param);
                }
                if (order.Status == OrderStatus.WaitBuyerPay)
                {
                    if (order.Unpaid > 0 && order.Unpaid<=order.Total)
                    {
                        var balance = 0m;
                        var amount = order.Unpaid;
                        if (useBalance)
                        {
                            var account = Account.Value.AccountGet(identity.Id);
                            if (account.AvailableMoney > 0 && account.AvailableMoney < amount)
                            {
                                balance = account.AvailableMoney;
                                amount = amount - balance;
                            }
                        }
                        var payment = new AccountPayment();
                        var eBank = PaymentEBankList().FirstOrDefault(o => o.Id == eBankId);
                        if (eBank != null)
                        {
                            var id = AccountPayment.NewId();
                            var payNo = id;
                            if (eBank.PlatformId == Platform.ChinaPay)
                            {
                                payNo = AccountPayment.NewPayNo();
                            }
                            payment.Id = id;
                            payment.PayNo = payNo;
                            payment.UserId = identity.Id;
                            payment.Category = PaymentCategory.Order;
                            payment.SubCategory = 0;
                            payment.ForeignKey = param;
                            payment.Amount = amount;
                            payment.Balance = balance;
                            payment.Method = PaymentMethod.EBank;
                            payment.EBankId = eBankId;
                            payment.PlatformId = eBank.PlatformId;
                            payment.Gateway = eBank.GatewayCode;
                            payment.BankCard = BankCard.Debit;
                            payment.BankName = eBank.PaymentBank.Name;
                            payment.CardNo = string.Empty;
                            payment.CardPwd = string.Empty;
                            payment.CardAmount = 0;
                            payment.IpAddress = GetIpAddress();
                            payment.ExternalNo = string.Empty;
                            payment.Flag = 0;
                            payment.Extra = string.Empty;
                            payment.Status = false;

                            payment.CreatedBy = User.Identity.Name;
                            payment.CreatedOn = time;
                            var status = Account.Value.AccountPaymentCreate(payment);
                            if (status)
                            {
                                var pay = GetOnlinePay(eBank.PlatformId);
                                url = pay.GetPayUrl(payment);
                            }
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        url = WebSite.My + "/order/detail/" + param;
                    }
                }
                else
                {
                    url = WebSite.My + "/order/detail/" + param;
                }
            }
            return Redirect(url);
        }
    }
}
